﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebSiteAdminTool.Web.DataAccess.Interfaces;
using System.Web.Security;

namespace WebSiteAdminTool.Web.DataAccess
{
    public class RolesDataAccess : BaseDataAccess, IRolesDataAccess
    {
        #region overriden base methods

        public override string ApplicationName
        {
            get
            {
                return Roles.ApplicationName;
            }
        }

        #endregion

        #region public methods

        public Guid GetRoleId(string roleName)
        {
            using (var context = base.GetDataContext())
            {
                return (from role in context.aspnet_Roles
                        where role.aspnet_Applications.ApplicationName == ApplicationName && role.RoleName == roleName
                        select role.RoleId)
                       .FirstOrDefault();
            }
        }

        public bool UpdateRole(Guid roleId, string roleName, string description)
        {
            using (var context = base.GetDataContext())
            {
                var currentRole = (from role in context.aspnet_Roles
                                   where role.RoleId == roleId && role.aspnet_Applications.ApplicationName == ApplicationName
                                   select role)
                                  .FirstOrDefault();

                if (null != currentRole)
                {
                    currentRole.RoleName = roleName;
                    currentRole.Description = description;
                }

                return context.SaveChanges() > 0;
            }
        }

        public aspnet_Roles[] GetRoles()
        {
            using (var context = base.GetDataContext())
            {
                var roles = (from role in context.aspnet_Roles
                             where role.aspnet_Applications.ApplicationName == ApplicationName
                             select role)
                             .ToArray();

                return roles;
            }
        }

        public Dictionary<Guid, int> GetRolesUsages()
        {
            using (var context = base.GetDataContext())
            {
                var roleUsages = (from role in context.aspnet_Roles
                                  where role.aspnet_Applications.ApplicationName == ApplicationName
                                  select new { role.RoleId, role.aspnet_Users.Count })
                                  .ToDictionary(r => r.RoleId, r => r.Count);

                return roleUsages;
            }
        }

        public bool RemoveRoleUsages(Guid roleId)
        {
            using (var context = base.GetDataContext())
            {
                var currentRole = (from role in context.aspnet_Roles
                                   where role.RoleId == roleId
                                   select role)
                                   .FirstOrDefault();

                if (null != currentRole && null != currentRole.aspnet_Users) 
                {
                    foreach (var roleUser in currentRole.aspnet_Users.ToArray())
                        context.DeleteObject(roleUser);
                }

                return context.SaveChanges() > 0;
            }
        }

        public bool RemoveRole(Guid roleId)
        {
            using (var context = base.GetDataContext())
            {
                var currentRole = (from role in context.aspnet_Roles
                                   where role.RoleId == roleId
                                   select role)
                                   .FirstOrDefault();

                if (null != currentRole)
                {
                    context.DeleteObject(currentRole);
                }

                return context.SaveChanges() > 0;
            }
        }

        #endregion
    }
}
